﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UColor.WPF;
using UCOLOR.Model;
using UColor.WPF.Internal;
namespace UCOLOR.View
{
    /// <summary>
    /// SearchView.xaml 的交互逻辑
    /// </summary>
    public partial class SearchView : Window
    {
        private string productCode;
        BaseControlModulesPage moduleWrapper;
        public SearchView(string product_code, BaseControlModulesPage mwrapper)
        {
            InitializeComponent();
            this.productCode = product_code;
            moduleWrapper = mwrapper;
            InitUI();
        }
        private void InitUI()
        {
            IList<SalesPart> list= DataHelper.getSalesPart(productCode, "");
            var dim = list.GroupBy(p => p.dim_quality);
            AddCheckBox(dim, 0);

            dim = list.GroupBy(p => p.cover_material);
            AddCheckBox(dim, 1);
            dim = list.GroupBy(p => p.inside_material);
            AddCheckBox(dim, 2);
            dim = list.GroupBy(p => p.inside_routing);
            AddCheckBox(dim, 3);
            dim = list.GroupBy(p => p.inside_paper);
            AddCheckBox(dim, 4);

            dim = list.GroupBy(p => p.inside_page);
            AddCheckBox(dim, 5);
        }
        private void AddCheckBox(IEnumerable< IGrouping<string, SalesPart>> dim,int iRow)
        {
            int iColumn = 2;
            foreach (IGrouping<string, SalesPart> s in dim)
            {
                CheckBox cb = new CheckBox();
                cb.Tag = iRow;
                cb.Content = s.Key;
                this.masterGrid.Children.Add(cb);
                cb.SetValue(Grid.ColumnProperty, iColumn);
                iColumn++;
                cb.SetValue(Grid.RowProperty, iRow);
                cb.Padding = new Thickness(2, 3, 0, 0);
            }

        }
        private string Where()
        {
           
            string Where = "";
            string []WhereField = new string[6];
            for(int i = 0; i <= 5; i++)
            {
                switch(i)
                {
                    case 0:
                        if ((bool)cbSize.IsChecked)
                        {
                            WhereField[0] = "";
                            continue;
                        }
                        else
                        {
                            for(int j = 0; j < this.masterGrid.Children.Count; j++)
                            {
                                if(this.masterGrid.Children[j] is CheckBox)
                                {
                                    CheckBox cb1 = this.masterGrid.Children[j] as CheckBox;
                                    if(cb1.Tag == null || !((bool)cb1.IsChecked))
                                        continue ;
                                    if(cb1.Tag.ToString() == "0")
                                    {
                                        if(string.IsNullOrEmpty(WhereField[0]))
                                            WhereField[0] = "dim_quality = '" + cb1.Content.ToString() + "'";
                                        else
                                            WhereField[0] = WhereField[0] + " or " + "dim_quality = '" + cb1.Content.ToString() + "'";
                                    }
                                }
                            }
                        }
                        break;
                    case 1:
                        if ((bool)cbCover.IsChecked)
                        {
                            WhereField[1] = "";
                            continue;
                        }
                        else
                        {
                            for(int j = 0; j < this.masterGrid.Children.Count; j++)
                            {
                                if(this.masterGrid.Children[j] is CheckBox)
                                {
                                    CheckBox cb1 = this.masterGrid.Children[j] as CheckBox;
                                    if(cb1.Tag == null || !((bool)cb1.IsChecked))
                                        continue ;
                                    if(cb1.Tag.ToString() == "1")
                                    {
                                        if(string.IsNullOrEmpty(WhereField[1]))
                                            WhereField[1] = "cover_material = '" + cb1.Content.ToString() + "'";
                                        else
                                            WhereField[1] = WhereField[1] + " or " + "cover_material = '" + cb1.Content.ToString() + "'";
                                    }
                                }
                            }
                        }
                        break;
                    case 2:
                        if ((bool)cbInnerCover.IsChecked)
                        {
                            WhereField[2] = "";
                            continue;
                        }
                         else
                        {
                            for(int j = 0; j < this.masterGrid.Children.Count; j++)
                            {
                                if(this.masterGrid.Children[j] is CheckBox)
                                {
                                    CheckBox cb1 = this.masterGrid.Children[j] as CheckBox;
                                    if (cb1.Tag == null || !((bool)cb1.IsChecked))
                                        continue ;
                                    if(cb1.Tag.ToString() == "2")
                                    {
                                        if (string.IsNullOrEmpty(WhereField[2]))
                                            WhereField[2] = "inside_material = '" + cb1.Content.ToString() + "'";
                                        else
                                            WhereField[2] = WhereField[2] + " or " + "inside_material = '" + cb1.Content.ToString() + "'";
                                    }
                                }
                            }
                        }
                        break;
                    case 3:
                        if ((bool)cbInnerTl.IsChecked)
                        {
                            WhereField[3] = "";
                            continue;
                        }
                         else
                        {
                            for(int j = 0; j < this.masterGrid.Children.Count; j++)
                            {
                                if(this.masterGrid.Children[j] is CheckBox)
                                {
                                    CheckBox cb1 = this.masterGrid.Children[j] as CheckBox;
                                    if (cb1.Tag == null || !((bool)cb1.IsChecked))
                                        continue ;
                                    if(cb1.Tag.ToString() == "3")
                                    {
                                        if (string.IsNullOrEmpty(WhereField[3]))
                                            WhereField[3] = "inside_routing = '" + cb1.Content.ToString() + "'";
                                        else
                                            WhereField[3] = WhereField[3] + " or " + "inside_routing = '" + cb1.Content.ToString() + "'";
                                    }
                                }
                            }
                        }
                        break;
                    case 4:
                        if ((bool)cbInnerIN.IsChecked)
                        {
                            WhereField[4] = "";
                            continue;
                        }
                         else
                        {
                            for(int j = 0; j < this.masterGrid.Children.Count; j++)
                            {
                                if(this.masterGrid.Children[j] is CheckBox)
                                {
                                    CheckBox cb1 = this.masterGrid.Children[j] as CheckBox;
                                    if (cb1.Tag == null || !((bool)cb1.IsChecked))
                                        continue ;
                                    if(cb1.Tag.ToString() == "4")
                                    {
                                        if (string.IsNullOrEmpty(WhereField[4]))
                                            WhereField[4] = "inside_paper = '" + cb1.Content.ToString() + "'";
                                        else
                                            WhereField[4] = WhereField[4] + " or " + "inside_paper = '" + cb1.Content.ToString() + "'";
                                    }
                                }
                            }
                        }
                        break;
                    case 5:
                        if ((bool)cbInnerPages.IsChecked)
                        {
                            WhereField[5] = "";
                            continue;
                        }
                        else
                        {
                            for (int j = 0; j < this.masterGrid.Children.Count; j++)
                            {
                                if (this.masterGrid.Children[j] is CheckBox)
                                {
                                    CheckBox cb1 = this.masterGrid.Children[j] as CheckBox;
                                    if (cb1.Tag == null || !((bool)cb1.IsChecked))
                                        continue;
                                    if (cb1.Tag.ToString() == "5")
                                    {
                                        if (string.IsNullOrEmpty(WhereField[5]))
                                            WhereField[5] = "inside_page = '" + cb1.Content.ToString() + "'";
                                        else
                                            WhereField[5] = WhereField[5] + " or " + "inside_page = '" + cb1.Content.ToString() + "'";
                                    }
                                }
                            }
                        }
                        break;
                }
            }
            foreach (string s in WhereField)
            {
                if (string.IsNullOrEmpty(Where))
                {
                    if (!string.IsNullOrEmpty(s))
                        Where = s;
                }
                else
                {
                    if (!string.IsNullOrEmpty(s))
                        Where = Where + " or " + s;

                }
            }
            return Where;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string where = Where();
            IList<SalesPart> list = DataHelper.getSalesPartWhere(where);
            UColor.Modules.SearchResultViewer view = new UColor.Modules.SearchResultViewer (list);
            view.ParentModulePage = moduleWrapper;
            moduleWrapper.OpenModule(view);
            this.Close();
        }
    }
}
